package view.transition
{
	import flash.system.System;
	
	import feathers.controls.ScreenNavigator;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.DisplayObject;
	
	import view.component.IAnimateNaviView;
	
	/**
	 * 大型怪物详细信息界面导航栏转场管理
	 * @author £iang
	 * 
	 */	
	public class AnimationNaviTransitionManager
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/**
		 * The <code>ScreenNavigator</code> being managed.
		 */
		protected var navigator:ScreenNavigator;
		
		/**
		 * @private
		 */
		protected var _activeTransition:Tween;
		
		/**
		 * @private
		 */
		protected var _savedOtherTarget:DisplayObject;
		
		/**
		 * @private
		 */
		protected var _savedCompleteHandler:Function;
		
		
		/**
		 * The duration of the transition, measured in seconds.
		 */
		public var duration:Number = 0.25;
		
		/**
		 * A delay before the transition starts, measured in seconds. This may
		 * be required on low-end systems that will slow down for a short time
		 * after heavy texture uploads.
		 */
		public var delay:Number = 1.8;
		
		public var creatDelay:Number = 1.2;
	
		/**
		 * Determines if the next transition should be skipped. After the
		 * transition, this value returns to <code>false</code>.
		 */
		public var skipNextTransition:Boolean = false;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		protected function onTransition(oldScreen:DisplayObject, newScreen:DisplayObject, onComplete:Function):void
		{
			if(!oldScreen && !newScreen)
			{
				throw new ArgumentError("Cannot transition if both old screen and new screen are null.");
			}
			
			if(this.skipNextTransition)
			{
				this.skipNextTransition = false;
				this._savedCompleteHandler = null;
				
				if(onComplete != null)
				{
					onComplete();
				}
				return;
			}
			
			if( oldScreen && oldScreen is IAnimateNaviView && newScreen is IAnimateNaviView ) 
			{
				navigator.touchable = false;
				Starling.juggler.delayCall( 
					function ():void
					{
						( oldScreen as IAnimateNaviView ).playHideAnimate();
					}, creatDelay
				);
			}
			
			if( newScreen is IAnimateNaviView ) 
			{
				var finalDelay:Number = 0;
				
				if ( oldScreen && oldScreen is IAnimateNaviView )
					finalDelay = delay;
				
				Starling.juggler.delayCall( 
					function ():void
					{
						( newScreen as IAnimateNaviView ).playShowAnimate();
						navigator.touchable = true;
						onComplete();
					}, finalDelay + creatDelay
				);
			}
			else
			{
				onComplete();
			}
		}
		
		/**
		 * 清除函数 
		 * 
		 */		
		public function clear():void
		{
			skipNextTransition = true;
			delay = 0;
			duration = 0;
			creatDelay = 0;
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function AnimationNaviTransitionManager( navigator:ScreenNavigator, transitDelay:Number = 1.8, creDelay:Number = 1.2 )
		{
			if(!navigator)
			{
				throw new ArgumentError("ScreenNavigator cannot be null.");
			}
			this.delay = transitDelay;
			this.creatDelay = creDelay;
			this.navigator = navigator;
			this.navigator.transition = this.onTransition;
		}
	}
}